package MtG.client;


import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.GroupLayout.Alignment;
import MtG.client.panels.CardPanel;
import MtG.client.panels.GamePanel;
import MtG.client.panels.InfoPanel;
import MtG.client.panels.dialogs.Connect;
import MtG.client.panels.dialogs.HelpInfo;
import MtG.client.panels.dialogs.NewPlayer;
import MtG.publicClass.Player;

/**
 * GUI.java is the main JFrame that is used to create the graphical user interface for the clients.
 * @author Marek Sirp
 * @author Jaan Sepp
 */
public class GUI extends JFrame{
	
    /**
	 * Determines if a de-serialized file is compatible with this class.
	 */
	private static final long serialVersionUID = -8837665669965025038L;

	//Creating the window.
    JFrame window = new JFrame(); 
    
    /**
     * Constant that holds the minimum width of the game window.
     */
    private final int WIDTH = 1024;
    /**
     * Constant that holds the minimum height of the game window.
     */
    private final int HEIGHT = 768;
    
    
    
    
    ///TestArea///Beginning
    ///Monitor resolution///
    /**
     * Initialize the toolkit used for getting monitor measures.
     */
    Toolkit toolkit =  Toolkit.getDefaultToolkit ();
    /**
     * Getting the dimensions of the monitor.
     */
    Dimension dim = toolkit.getScreenSize();
    /**
     * Field for holding the height of the current monitor.
     */
    //private final double MONITORHEIGHT = dim.height;
    /**
     * Field for holding the width of the current monitor.
     */
    //private final double MONITORWIDTH = dim.width;
    ///TestArea///End   
    NewPlayer dialog;
    
    

    
    //Creating main panels.
	CardPanel cardPanel = new CardPanel();
	GamePanel gamePanel = new GamePanel();
	InfoPanel infoPanel = new InfoPanel();
	
    /**
     * Constant that holds the fixed height of the panel where players cards in hand are shown.
     */
    private final int CARDPANELHEIGHT = 175;
    /**
     * Constant that holds the width of  the infopanel on right side of the window.
     */
    private final int INFOPANELWIDTH = 200;
	
    //Creating the menu bar object.
    private JMenuBar menuBar = new JMenuBar();
    
    //Creating the menu drop down menus
    private JMenu connectMenu = new JMenu("Connection");
    private JMenu playerMenu = new JMenu("Player");
    private JMenu gameMenu = new JMenu("Game");
    
    
    //Creating the drop down menu objects.
    private JMenuItem connectMenuItemConnect = new JMenuItem("Connect");
    private JMenuItem connectMenuItemDisconnect = new JMenuItem("Disconnect");
    private JMenuItem playerMenuItemName = new JMenuItem("Change name");
    private JMenuItem gameMenuItemNewGame = new JMenuItem("New Game");
    private JMenuItem gameMenuItemHelp = new JMenuItem("Help");
    
    /**
     *Field that holds the reference to the current client.
     */
    private ClientConnection client;  
    
    /**
     * Create new player
     */
    private Player p = new Player();
    
	GUI gui = this;
    
	/**
	 * Constructor for the main window.
	 */
	public GUI() {
		
        while(p.getName().equals("") || p.getDeck() == null){
        	dialog = new NewPlayer(this, p);
        }
        p = dialog.getPlayer();
        
        setTitle("Magic the Gathering DEMO");
        setBounds(0, 0,WIDTH, HEIGHT);
        setMinimumSize(getSize());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
		 //Adding menu items with listeners to the menu bar.
		setJMenuBar(menuBar);
        menuBar.add(connectMenu);
        menuBar.add(playerMenu);
        menuBar.add(gameMenu);
        connectMenu.add(connectMenuItemConnect);
        connectMenu.add(connectMenuItemDisconnect);
        playerMenu.add(playerMenuItemName);
        gameMenu.add(gameMenuItemNewGame);
        gameMenu.add(gameMenuItemHelp);
        ListenMenuQuit listen = new ListenMenuQuit();
        connectMenuItemConnect.addActionListener(listen);
        connectMenuItemDisconnect.addActionListener(listen);
        playerMenuItemName.addActionListener(listen);
        gameMenuItemNewGame.addActionListener(listen);
        gameMenuItemHelp.addActionListener(listen);
        
        //testing
    	cardPanel.placeDeck(p);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(gamePanel)
						.addComponent(cardPanel))
					.addGap(0)
					.addComponent(infoPanel, GroupLayout.PREFERRED_SIZE, INFOPANELWIDTH, GroupLayout.PREFERRED_SIZE)
					)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addComponent(gamePanel)	
					.addGap(0)
					.addComponent(cardPanel, GroupLayout.PREFERRED_SIZE, CARDPANELHEIGHT, GroupLayout.PREFERRED_SIZE))
				.addComponent(infoPanel)
		);
		
		getContentPane().setLayout(groupLayout);

	}
	
    /**
     * Class that holds all the processes needed to be done when a certain item from the JMenu is chosen.
     * @author Marek Sirp
     * @author Jaan Sepp
     */
    public class ListenMenuQuit implements ActionListener { 
        public void actionPerformed(ActionEvent e){
                if(e.getActionCommand().equals(connectMenuItemConnect.getActionCommand())) {
                        if(client == null || !client.isAlive()){
                        		Connect connect = new Connect(gui);
                        		if(!connect.getAddress().isEmpty()){
                        			client = new ClientConnection(p, cardPanel, infoPanel, gamePanel, connect.getAddress());
                        			Thread t = client;
                        			t.start();
                        		}
                        }
                } else if(e.getActionCommand().equals(connectMenuItemDisconnect.getActionCommand())){
                        if(client != null){
                                client.disconnect();
                        }
                        System.out.println(connectMenuItemDisconnect.getActionCommand());
                } else if(e.getActionCommand().equals(playerMenuItemName.getActionCommand())){
                        System.out.println(playerMenuItemName.getActionCommand());
                } else if(e.getActionCommand().equals(gameMenuItemNewGame.getActionCommand())){
                        System.out.println(gameMenuItemNewGame.getActionCommand());
                }  else {
                        System.out.println(gameMenuItemHelp.getActionCommand());
                        @SuppressWarnings("unused")
						HelpInfo hi = new HelpInfo(window);
                }
        }
}
    /**
     * Method that makes the frame (main window) visible.
     */
    public void launchFrame(){            
        setVisible(true);
    } 
       
    /**
     * @return retrieves this clientConnection.
     */
    public ClientConnection getClientConnection(){
    	return client;
    }
}


